from pony import orm
from datetime import datetime
import pytz

from .config import settings


# orm.set_sql_debug(True)


db = orm.Database()
db.bind(
    provider='postgres',
    host=settings.PG_HOST_ALG,
    port=settings.PG_PORT_ALG,
    database=settings.PG_DATABASE_ALG,
    user=settings.PG_USERNAME_ALG,
    password=settings.PG_PASSWORD_ALG,
)


################
# place all entities below
################

# Required, 必须有值的字段, 值不能是null或者''
# Optional, 值可以为null或者''的字段, 字符字段默认为'', 其它字段默认为null

class AlgBill(db.Entity):
    _table_ = 'inphile_alg_bill'

    id = orm.PrimaryKey(str)
    app_name = orm.Required(str)
    operator = orm.Required(str)
    status = orm.Optional(int)
    case_id = orm.Optional(str)
    image_id = orm.Required(str)
    image_path = orm.Required(str)
    bill_code = orm.Optional(str)
    create_time = orm.Required(datetime)
    update_time = orm.Optional(datetime)


class AlgBillHistory(db.Entity):
    _table_ = 'inphile_alg_bill_history'

    id = orm.PrimaryKey(str)
    bill_id = orm.Required(str)
    person = orm.Required(str)
    action = orm.Required(str)
    create_time = orm.Required(datetime)


class AlgEInvoice(db.Entity):
    _table_ = 'inphile_alg_e_invoice'

    id = orm.PrimaryKey(str)
    image_id = orm.Required(str)
    image_path = orm.Required(str)
    e_invoice_code = orm.Required(str)
    e_invoice_number = orm.Required(str)

################
# place all entities above
################


# all entities connected to the database must be defined before calling generate_mapping()
# if the tables do not already exist, then they will be created
db.generate_mapping()
